package tako.wisers.com.android.apis.activity;

import com.google.android.maps.GeoPoint;

import tako.wisers.com.android.apis.R;
import tako.wisers.com.android.apis.adapter.ListManager;
import tako.wisers.com.android.apis.event.CallPhoneEvent;
import tako.wisers.com.android.apis.event.Event;
import tako.wisers.com.android.apis.event.EventBus;
import tako.wisers.com.android.apis.event.MapRouteEvent;
import tako.wisers.com.android.apis.handler.CallPhoneHandler;
import tako.wisers.com.android.apis.handler.MapRouteHandler;
import tako.wisers.com.android.apis.item.StationItem;
import tako.wisers.com.android.apis.location.GPSManager;
import tako.wisers.com.android.apis.location.RouteDistanceCount;
import tako.wisers.com.android.apis.util.BackGroundHelper;
import tako.wisers.com.android.apis.util.Global;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class StationInfoActivity extends Activity {
	private static final int MENU_RADAR = Menu.FIRST + 1;

//	private static final int MENU_MAP = Menu.FIRST + 2;

	private static final int MENU_VIEW = Menu.FIRST + 4;

	private static final int DIALOG_NO_RADAR = 1;

	private StationItem mItem;
	
	private EventBus mBus;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.station_info);
		System.gc();
		mBus = EventBus.getInstance();

		mItem = getIntent().getParcelableExtra(Global.ITEM_KEY);
		String title = mItem.getTitle();
		String address = mItem.getOwner();
		String phone = mItem.getPhone();
		String content = mItem.getInfo();
		Bitmap image = mItem.getBitmap();
		GeoPoint endPoint = mItem.getLocation();

		TextView titletext = (TextView) findViewById(R.id.s_title);
		TextView addresstext = (TextView) findViewById(R.id.address);
		TextView phonetext = (TextView) findViewById(R.id.phone);
		TextView lineDistanceTxt = (TextView) findViewById(R.id.line_distance);
		TextView contenttext = (TextView) findViewById(R.id.info);
		
		titletext.setText(title);
		addresstext.setText(address);
		phonetext.setText("Tel. " + phone);
		lineDistanceTxt.setText(setDistance(endPoint));
		contenttext.setText(content);
		
		initCallButtonEvent(R.id.call);
		intRouteButton(R.id.walk_route);
		intRouteButton(R.id.drive_route);
		
		ImageView imageView = (ImageView) findViewById(R.id.station_image);
		if (image != null) {
			imageView.setImageBitmap(image);
			imageView.setVisibility(View.VISIBLE);
		} else {
			imageView.setVisibility(View.GONE);
		}
		BackGroundHelper.settingBackGround(this, R.layout.station_info);
	}
	
	private String setDistance(GeoPoint endPoint){
		GPSManager gm = GPSManager.getInstance();
		GeoPoint point = gm.getGPSCenter();
		int lat1 = point.getLatitudeE6();
		int lng1 = point.getLongitudeE6();
		int lat2 = endPoint.getLatitudeE6();
		int lng2 = endPoint.getLongitudeE6();
		double distance = RouteDistanceCount.getDistance(lat1, lng1, lat2, lng2);
		distance = Math.round(distance*100)/100d;
		String title = getString(R.string.line_distance_title);
		String str = title + distance + "km";
		return str;
	}

	private void initCallButtonEvent(int id) {
		View callButton = findViewById(id);
		mBus.addListener(new Event(R.id.call, Event.EVENT_CLICK), new CallPhoneHandler(this));
		callButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				mBus.executeEvent(new CallPhoneEvent(v.getId(), Event.EVENT_CLICK, mItem.getPhone()));
			}
		});
	}
	
	private void intRouteButton(int id){
		View button = findViewById(id);
		mBus.signinEvent(id, new MapRouteHandler(this), Event.EVENT_CLICK);
		button.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				MapRouteEvent event = new MapRouteEvent(v.getId(), Event.EVENT_CLICK, mItem.getLocation());
				mBus.executeEvent(event);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_RADAR, 0, R.string.menu_radar).setIcon(
				R.drawable.ic_menu_radar).setAlphabeticShortcut('R');
//		menu.add(0, MENU_MAP, 0, R.string.menu_map).setIcon(
//				R.drawable.ic_menu_map).setAlphabeticShortcut('M');
//		menu.add(0, MENU_PHONE, 0, R.string.phone_text).setIcon(
//				R.drawable.ic_menu_author).setAlphabeticShortcut('P');
		menu.add(0, MENU_VIEW, 0, R.string.menu_view).setIcon(
				android.R.drawable.ic_menu_view).setAlphabeticShortcut('V');
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		System.gc();
		switch (item.getItemId()) {
		case MENU_RADAR: {
			// Launch the radar activity (if it is installed)
			Intent i = new Intent("com.google.android.radar.SHOW_RADAR");
			GeoPoint location = mItem.getLocation();
			i.putExtra("latitude",
					(float) (location.getLatitudeE6() / 1000000f));
			i.putExtra("longitude",
					(float) (location.getLongitudeE6() / 1000000f));
			try {
				startActivity(i);
			} catch (ActivityNotFoundException ex) {
				showDialog(DIALOG_NO_RADAR);
			}
			return true;
		}
		case MENU_VIEW: {
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setData(Uri.parse(mItem.getURL()));
			startActivity(i);
			return true;
		}
		}

		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			Intent intent = new Intent(Intent.ACTION_EDIT);
			startActivity(intent);
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
